package com.china.cnlongdb.server.connect.nettymission.operator;

import java.util.Map;
import java.util.concurrent.CountDownLatch;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.china.cnlongdb.basic.enums.sysnc.LongdbDataSyncEnms;
import com.china.cnlongdb.server.connect.nettymission.LongdbNettyClient;

/**
 * 系统名称：ExecSendNettyRequestThread
 * 模块名称：
 * 模块描述：多线程发送Netty请求
 * 功能列表：
 * 模块作者：zouyong
 * 开发时间：2019年4月30日 下午4:22:19
 * 模块路径：com.china.cnlongdb.server.connect.nettymission.operator.SendNettyRequest
 */
public class SendNettyRequest extends Thread {

	private final Logger logger = (Logger) LoggerFactory.getLogger(SendNettyRequest.class);

	// 传输服务IP
	private String serverIp;
	// 传输服务端口
	private int serverPort;
	// 需要发送到服务端的消息内容
	private String message;
	// 服务端返回的对象
	private Map<String, String> returnMap;
	// 用于同步返回的线程对象值 
	private CountDownLatch doneSignal;
	
	public SendNettyRequest(String serverIp, int serverPort, String message, Map<String, String> returnMap,
			CountDownLatch doneSignal) {
		this.serverIp = serverIp;
		this.serverPort = serverPort;
		this.message = message;
		this.returnMap = returnMap;
		this.doneSignal = doneSignal;
	}
	
	@Override
	public void run() {
		try {
			// 执行发送Netty网络通信请求
			new LongdbNettyClient(serverIp, serverPort, message, returnMap, doneSignal);
			returnMap.put(LongdbDataSyncEnms.returnCode.name(), LongdbDataSyncEnms.success.name());
		}
		catch(Exception e) {
			logger.error("执行发送Netty网络通信请求异常：{}", e.getMessage());
			returnMap.put(LongdbDataSyncEnms.returnObject.name(), e.getMessage());
			returnMap.put(LongdbDataSyncEnms.returnCode.name(), LongdbDataSyncEnms.fail.name());
		}
		finally {
			doneSignal.countDown();
		}
	}

}
